<?php
require_once dirname(__FILE__) . '/../../../incls/php/dbutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/miscutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/config.php';
$page_id = MiscUtils::getParam('i', 0);
$inst_id = MiscUtils::getParam('inid', NULL);
$comp_track_id = MiscUtils::getParam('comp_t', MiscUtils::unique());
$myPdo = DbUtils::createPdoInst();
$cond_vals = new stdClass();
$cond_vals->c = 't.comp_track_id = :v1';
$cond_vals->v = array(':v1' => $comp_track_id);
$items = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'comp_prize', $cond_vals, 't.order_number', 'ASC', NULL, NULL, NULL)->d;
$pics = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'comp_pic', $cond_vals, 't.id', 'DESC', NULL, NULL, NULL)->d;
$cond_vals = new stdClass();
$cond_vals->c = 't.track_id = :v1';
$cond_vals->v = array(':v1' => $comp_track_id);
$comps = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'comp', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
if (count($comps) > 0) {
  $comp = $comps[0];
} else {
  $comp = new stdClass();
  $comp->track_id = $comp_track_id;
  $comp->title = '';
  $comp->description = '';
  $comp->is_finished = 0;
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<style>
* {
  font-family: Trebuchet MS;
  font-size: 12px;
  outline: 0;
  padding: 0;
  margin: 0;
}
table.images {
  display: inline;
}
table.images td {
  border: none !important;
}
table.list {
  border-left: 1px solid #ddd;
  border-top: 1px solid #ddd;
  font-size: 12px;
  width: 350px;
}
table.list tr.none td {
  padding: 0 5px;
  background-color: #fff;
  border-right: 1px solid #ddd;
  border-bottom: 1px solid #ddd;
  word-wrap: break-word;
}
table.list td.rowodd {
  padding: 0 5px;
  background-color: #eee;
  border-right: 1px solid #ddd;
  border-bottom: 1px solid #ddd;
  word-wrap: break-word;
  vertical-align: top;
  padding-top: 2px;
}
table.list td.roweven {
  padding: 0 5px;
  background-color: #fff;
  border-right: 1px solid #ddd;
  border-bottom: 1px solid #ddd;
  word-wrap: break-word;
  vertical-align: top;
  padding-top: 2px;
}
table.list td.rowodd:hover {
  background-color: #f4f3b2;
}
table.list td.roweven:hover {
  background-color: #f4f3b2;
}
table.form {
  border: 1px solid #ddd;
  width: 100%;
}
table.form td {
  background-color: #fff;
  vertical-align: top;
  border: 1px solid #ddd;
}
table.form td.title {
  background-color: #f1d0d0;
  border-top: 1px solid #fff;
}
table.form td.content textarea {
  margin: 0;
  padding: 0;
}
table.form td.content input {
  width: 95%;
  height: 25px;
  border: 0;
  padding: 2px;
}
.imgbtn{
  background-color: #fff;
  border-bottom: 2px solid #666;
  border-top: 1px solid #666;
  border-right: 1px solid #666; 
  border-left: 1px solid #666;
  font-weight: bold;
  cursor: pointer;
  display: table-cell;
  width: 120px;
  height: 35px;
  text-align: center;
  vertical-align: middle;
  font-size: 14px;
}
</style>
</head>
<body>
<table cellspacing="0" cellpadding="0" style="padding-bottom: 5px;">
  <tr>
    <td><div class="imgbtn" onclick="parent.location.href='http://<?php echo URL . 'admin.php?i=' . $page_id; ?>'">所有比赛</div></td>
    <td style="padding: 0 10px; vertical-align: middle;"><b>Selected</b><br><img src="images/right.gif" alt=""></img></td>
    <td>
      <div class="imgbtn"
        <?php if (isset($comp->id)) { ?>
        >编辑比赛
        <?php } else { ?>
        >新比赛
        <?php } ?>
      </div>
    </td>
  </tr>
</table>
<table cellspacing="0" cellpadding="0">
<tr>
<td style="vertical-align: top;">
<table class="form">
<tr>
<td class="title" style="width: 120px;">比赛标题 </td>
<td class="content"><input id="title" type="text" value="<?php echo $comp->title; ?>" onchange="_comp.title=this.value;"></input></td>
</tr>
<tr>
<td class="title" style="width: 120px;">是否结束</td>
<td style="text-align: left;"><input id="is_finished" type="checkbox" <?php if($comp->is_finished == 1) echo 'checked="checked"'; ?>  onchange="_comp.is_finished=this.checked;"></input></td>
</tr>
<tr>
<td class="title">比赛规则</td>
<td class="content"><textarea id="description"><?php echo $comp->description; ?></textarea></td>
</tr>
<tr>
<td class="title"><span id="upimg"></span><span id="canimg" style="display: none;"></span><br></br>参赛样片 (6x4比例)<br></br>长宽不要超过950像素</td>
<td class="content" style="height: 300px; width: 400px;" id="images"></td>
</tr>
</table>
<button onclick="save();">Save</button>
</td>
<td style="vertical-align: top; padding-left: 10px;">
<b>奖品列表</b><br></br>
<button onclick="add_prize();">New Prize</button>
<button onclick="update_prize();">Update</button>
(200x200 image)
<table id="items" class="list" cellspacing="0" cellpadding="0"></table>
</td>
</tr>
</table>
<script type="text/javascript" src="../../../libs/jquery/jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="../../../incls/js/myutils.js"></script>
<script type="text/javascript" src="../../../libs/tiny_mce/tiny_mce.js"></script>
<script type="text/javascript" src="../../../libs/swfupload/swfupload.js"></script>
<script type="text/javascript" src="../../../libs/swfupload/swfupload.queue.js"></script>
<script type="text/javascript" src="../../../libs/swfupload/myupload.js"></script>
<script type="text/javascript" src="../../../incls/js/pagination.js"></script>

<script type="text/javascript">
  var _items = JSON.parse('<?php echo addslashes(json_encode($items)); ?>');
  var _pics = JSON.parse('<?php echo addslashes(json_encode($pics)); ?>');
  var _comp = JSON.parse('<?php echo addslashes(json_encode($comp)); ?>');
  
  $(function(){
    tinyMCE.init( {
      // General options
      mode: 'exact',
      elements: 'description',
      theme: 'advanced',

      plugins: 'pdw,filebrowser,autolink,lists,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,wordcount,advlist',
      // Theme options
      theme_advanced_buttons1: 'pdw_toggle,|,undo,redo,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,fontselect,fontsizeselect,|,forecolor,backcolor,|,image,media,|,preview',
      theme_advanced_buttons2: 'search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,link,unlink,anchor,cleanup,help,code,|,insertdate,inserttime',
      theme_advanced_buttons3: 'tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,advhr,|,print,|,ltr,rtl,|,fullscreen',
      theme_advanced_buttons4: 'insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak',
      theme_advanced_toolbar_location: 'top',
      theme_advanced_toolbar_align: 'left',
      theme_advanced_statusbar_location: 'bottom',
      theme_advanced_resizing: false,

      file_browser_callback: 'filebrowser',
      pdw_toggle_on: '1',
      pdw_toggle_toolbars: '2,3,4',
      // Example content CSS (should be your site CSS)
      theme_advanced_font_sizes: '10px,11px,12px,13px,14px,15px,16px,17px,18px,19px,20px,21px,22px,23px,24px,25px,26px,27px,28px,29px,30px',
      theme_advanced_fonts : "黑体=黑体;"+
                             "宋体=宋体;"+
                             "Andale Mono=andale mono,times;"+
                             "Arial=arial,helvetica,sans-serif;"+
                             "Arial Black=arial black,avant garde;"+
                             "Book Antiqua=book antiqua,palatino;"+
                             "Comic Sans MS=comic sans ms,sans-serif;"+
                             "Courier New=courier new,courier;"+
                             "Georgia=georgia,palatino;"+
                             "Helvetica=helvetica;"+
                             "Impact=impact,chicago;"+
                             "Symbol=symbol;"+
                             "Tahoma=tahoma,arial,helvetica,sans-serif;"+
                             "Terminal=terminal,monaco;"+
                             "Times New Roman=times new roman,times;"+
                             "Trebuchet MS=trebuchet ms,geneva;"+
                             "Verdana=verdana,geneva;"+
                             "Webdings=webdings;"+
                             "Wingdings=wingdings,zapf dingbats"
    });

    new MyUpload('upimg', 'upload_pic.php', function(msgs) {
      for (var idx in msgs) {
        var obj = msgs[idx];
        _pics[_pics.length] = JSON.parse(obj);
      }
      update_pics();
    }, {
      dir: '../../../',
      button_image_url: 'images/browse.png',
      button_width: 94,
      button_height: 25,
      file_size_limit: '8MB',
      file_types: '*.jpg;*.jpeg;*.png;*.gif',
      file_types_description: 'Image Files',
      cancelBtnId: 'canimg',
      post_params: {
        comp_t: '<?php echo $comp_track_id; ?>'
      },
      debug: 0
    });

    update_pics();
    
    var table, tr, td;
    table = document.getElementById('items');
    MyUtils.removeTableRows(table, 0);
    for (var idx in _items) {
      var obj = _items[idx];
      tr = table.insertRow(-1);
      td = tr.insertCell(-1);
      td.className = (idx % 2 == 0) ? 'roweven' : 'rowodd';
      var itable, itr, itd, ielm;
      itable = document.createElement('table');
      td.appendChild(itable);
      itable.className = 'form';
      itr = itable.insertRow(-1);
      itd = itr.insertCell(-1);
      itd.className = 'title';
      itd.appendChild(document.createTextNode('#' + (parseInt(idx, 10) + 1)));
      ielm = document.createElement('input');
      ielm.type = 'submit';
      ielm.value = 'Delete';
      ielm._obj = obj;
      ielm.onclick = function() {
        remove_prize(this._obj.id);
      }
      itd.appendChild(ielm);
      itd = itr.insertCell(-1);
      itd.className = 'content';
      ielm = document.createElement('input');
      ielm.type = 'text';
      ielm.value = obj.order_number;
      ielm._obj = obj;
      ielm.onchange = function() {
        this._obj.order_number = this.value;
      }
      itd.appendChild(ielm);
      itr = itable.insertRow(-1);
      itd = itr.insertCell(-1);
      itd.className = 'title';
      itd.appendChild(document.createTextNode('Title'));
      itd = itr.insertCell(-1);
      itd.className = 'content';
      ielm = document.createElement('input');
      ielm.type = 'text';
      ielm.value = obj.title;
      ielm._obj = obj;
      ielm.onchange = function() {
        this._obj.title = this.value;
      }
      itd.appendChild(ielm);
      itr = itable.insertRow(-1);
      itd = itr.insertCell(-1);
      itd.className = 'title';
      itd.appendChild(document.createTextNode('Price'));
      itd = itr.insertCell(-1);
      itd.className = 'content';
      ielm = document.createElement('input');
      ielm.type = 'text';
      ielm.value = obj.price;
      ielm._obj = obj;
      ielm.onchange = function() {
        this._obj.price = this.value;
      }
      itd.appendChild(ielm);
      itr = itable.insertRow(-1);
      itd = itr.insertCell(-1);
      itd.className = 'title';
      ielm = document.createElement('span');
      ielm.id = 'up' + obj.id;
      itd.appendChild(ielm);
      ielm = document.createElement('span');
      ielm.id = 'can' + obj.id;
      ielm.style.display = 'none';
      itd.appendChild(ielm);
      itd = itr.insertCell(-1);
      itd.className = 'content';
      itd.style.height = '100px';
      itd.id = 'img' + obj.id;
      elm = document.createElement('img');
      elm.src = obj.path;
      elm.style.width = '100px';
      itd.appendChild(elm);

      new MyUpload('up' + obj.id, 'upload_prize.php', function(msgs) {
        var obj, elm, div;
        obj = JSON.parse(msgs);
        div = document.getElementById('img' + obj.id);
        MyUtils.removeChildElements(div);
        elm = document.createElement('img');
        elm.src = obj.path;
        elm.style.width = '100px';
        div.appendChild(elm);
      }, {
        dir: '../../../',
        button_image_url: 'images/browse.png',
        button_width: 94,
        button_height: 25,
        file_upload_limit: 1,
        file_queue_limit: 1,
        file_size_limit: '8MB',
        file_types: '*.jpg;*.jpeg;*.png;*.gif',
        file_types_description: 'Image Files',
        cancelBtnId: 'can' + obj.id,
        post_params: {
          id: obj.id
        },
        debug: 0
      });
    }
    if (_items.length == 0) {
      tr = table.insertRow(-1);
      tr.className = 'none';
      td = tr.insertCell(-1);
      td.appendChild(document.createTextNode('No Prizes'));
    }
    parent.updateHeight_<?php echo $inst_id; ?>();
  });

  function update_pics() {
    var div = document.getElementById('images');
    MyUtils.removeChildElements(div);
    for (var idx in _pics) {
      var obj = _pics[idx];
      add_pic(div, obj)
    }
  }

  function add_pic(div, obj) {
    var elm, table, tr, td;
    table = document.createElement('table');
    table.className = 'images';
    div.appendChild(table);
    tr = table.insertRow(-1);
    td = tr.insertCell(-1);
    elm = document.createElement('img');
    elm.src = obj.path;
    elm.style.width = '100px';
    td.appendChild(elm);
    td = tr.insertCell(-1);
    td.style.verticalAlign = 'top';
    elm = document.createElement('img');
    elm.src = 'images/delete24.png';
    elm.style.cursor = 'pointer';
    elm._obj = obj;
    elm.onclick = function() {
      remove_pic(this._obj.id);
    }
    td.appendChild(elm);
  }

  function add_prize() {
    var obj = {
      comp_track_id: '<?php echo $comp_track_id; ?>',
      order_number: 0,
      title: 'Untitled',
      price: 0.00,
      size: 0,
      path: ''
    };
    $.ajax({
      type: 'POST',
      url: '<?php echo basename(__FILE__, '.php'); ?>.server.php',
      data: 'action=add_prize&q=' + encodeURIComponent(JSON.stringify(obj)),
      success: function(msg){
        try {
          parent.location.href = 'http://<?php echo URL?>admin.php?i=<?php echo $page_id; ?>&script=index_comp&comp_t=<?php echo $comp_track_id; ?>';
        } catch (ex) {
        }
      }
    });
  }
  
  function update_prize() {
    var items = [];
    for (var idx in _items) {
      var obj = _items[idx];
      items[items.length] = {
        id: obj.id,
        order_number: obj.order_number,
        title: obj.title,
        price: obj.price
      };
    }
    $.ajax({
      type: 'POST',
      url: '<?php echo basename(__FILE__, '.php'); ?>.server.php',
      data: 'action=update_prize&q=' + encodeURIComponent(JSON.stringify(items)),
      success: function(msg){
        try {
          parent.location.href = 'http://<?php echo URL?>admin.php?i=<?php echo $page_id; ?>&script=index_comp&comp_t=<?php echo $comp_track_id; ?>';
        } catch (ex) {
        }
      }
    });
  }
  
  function remove_prize(id) {
    $.ajax({
      type: 'POST',
      url: '<?php echo basename(__FILE__, '.php'); ?>.server.php',
      data: 'action=remove_prize&q=' + encodeURIComponent(id),
      success: function(msg){
        try {
          parent.location.href = 'http://<?php echo URL?>admin.php?i=<?php echo $page_id; ?>&script=index_comp&comp_t=<?php echo $comp_track_id; ?>';
        } catch (ex) {
        }
      }
    });
  }

  function remove_pic(id) {
    $.ajax({
      type: 'POST',
      url: '<?php echo basename(__FILE__, '.php'); ?>.server.php',
      data: 'action=remove_pic&q=' + encodeURIComponent(id),
      success: function(msg){
        try {
          parent.location.href = 'http://<?php echo URL?>admin.php?i=<?php echo $page_id; ?>&script=index_comp&comp_t=<?php echo $comp_track_id; ?>';
        } catch (ex) {
        }
      }
    });
  }

  function save() {
    if (_comp.title == '') {
      window.alert('标题不能为空白');
      return;
    }
    _comp.description = tinyMCE.get('description').getContent();
    $.ajax({
      type: 'POST',
      url: '<?php echo basename(__FILE__, '.php'); ?>.server.php',
      data: 'action=save&q=' + encodeURIComponent(JSON.stringify(_comp)),
      success: function(msg){
        try {
          parent.location.href = 'http://<?php echo URL?>admin.php?i=<?php echo $page_id; ?>&script=index_comp&comp_t=<?php echo $comp_track_id; ?>';
        } catch (ex) {
        }
      }
    });
  }
</script>
</body>
</html>